<template>
  <div>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      label-width="120px"
      class="demo-ruleForm"
      size="default"
      status-icon
    >
      <el-form-item label="关联专题：">
        <el-transfer
          v-model="value"
          filterable
          :filter-method="filterMethod"
          filter-placeholder="State Abbreviations"
          :data="data"
          :titles="['待选择', '已选择']"
        />
      </el-form-item>
      <el-form-item label="关联优选：">
        <el-transfer
          v-model="valuetwo"
          filterable
          :filter-method="filterMethodtwo"
          filter-placeholder="State Abbreviations"
          :data="datatwo"
          :titles="['待选择', '已选择']"
        />
      </el-form-item>
      <!-- 按钮 -->
      <el-button
        style="margin-left: 130px; margin-top: 5px"
        @click="chanddis"
        type="primary"
        >上一步，填写商品促销</el-button
      >
      <!--    @click="chandnext" -->
      <el-button
        @click="chandnext"
        style="margin-left: 180px; margin-top: 5px"
        type="primary"
        >添加商品</el-button
      >
      {{ valuetwo }}
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import * as api from "@/request/http";

// 表单的校验
interface RuleForm {
  subjectProductRelationList: any;
  prefrenceAreaProductRelationList: any;
}

// 表单数据
const ruleForm = reactive<RuleForm>({
  subjectProductRelationList: [],
  prefrenceAreaProductRelationList: [],
});

// 穿梭狂数据
const data = ref<any>();
const value = ref([]);
// 请求专题的全部商品专题
api.addProduct.subjectListAll().then((res) => {
  console.log(res);
  data.value = res.data.map((item) => {
    return {
      key: item.id,
      label: item.title,
      initial: item.title,
    };
  });
});
const filterMethod = (query: any, item: any) => {
  ruleForm.subjectProductRelationList = value.value.map((item: any) => {
    return {
      id: item,
    };
  });
  return item.initial.includes(query);
};

// 第二个穿梭框
const datatwo = ref<any>();
const valuetwo = ref([]);
// 请求专题的全部商品专题
api.addProduct.prefrenceAreaListAll().then((res) => {
  console.log(res);
  datatwo.value = res.data.map((item) => {
    return {
      key: item.id,
      label: item.name,
      initial: item.name,
    };
  });
});
const filterMethodtwo = (query: any, item: any) => {
  ruleForm.prefrenceAreaProductRelationList = valuetwo.value.map(
    (item: any) => {
      return {
        id: item,
      };
    }
  );
  return item.initial.includes(query);
};

// 点击按钮跳转
// 表单的传值
// 接收父组件传过来的方法
const emits = defineEmits<{
  (e: "bingnext", a: number): void;
  (e: "fromNext", a: object): void;
}>();
// 点击上一页回到上一页
const chanddis = () => {
  emits("bingnext", 2);
};
// 点击按钮,传值到下一页面
const chandnext = () => {
  // emits("fromNext", ruleForm);
  emits("fromNext", ruleForm);
  emits("bingnext", 4);
};
</script>

<style scoped></style>
